package com.uduemc.biso.web.component;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException exception) throws IOException, ServletException {

		String parameter = request.getParameter("username");

		String string = exception.getClass().toString();
		logger.info("登录失败执行的动作，异常类：" + string);
		response.sendRedirect(
				"/login?error=" + string.substring(string.lastIndexOf(".") + 1).toLowerCase().replace("exception", "")
						+ "&username=" + parameter + "&time=" + String.valueOf(new Date().getTime()));
	}

}
